package com.hud.easynavi;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.TypedValue;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建时间：16/5/13 16:26
 * 项目名称：HudSource
 *
 * @author lingxiang.wang
 * @email lingxiang.wang@alibaba-inc.com
 * 类说明：
 */

public class NaviUtil {
    public enum DistanceUnit{
        Chinese,English
    }

    public static int getDrawableResourceId(Context context, String resourceId) {
        return context.getResources()
                      .getIdentifier(resourceId,
                           "drawable", context.getPackageName());
    }

    public static String formatKM(int d) {
        if (d == 0)
            return "0米";
        if (d < 100)
            return d + "米";
        if ((100 <= d) && (d < 1000))
            return d + "米";
        if ((1000 <= d) && (d < 10000))
            return (d / 10) * 10 / 1000.0D + "公里";
        if ((10000 <= d) && (d < 100000)) {
            return (d / 100) * 100 / 1000.0D + "公里";
        }
        return (d / 1000) + "公里";
    }

    public static String formatTime(int time) {
        time = Math.abs(time);
        StringBuffer sb = new StringBuffer();
        int tmp = time / 3600;
        if (tmp == 0) {
            sb.append("00:");
        }
        if (tmp > 0) {
            sb.append(formatTime1(tmp) + ":");
        }
        tmp = time % 3600;
        int tmp1 = (tmp + 59) / 60;
        sb.append(formatTime1(tmp1) + ":");

        tmp1 = tmp % 60;
        sb.append(formatTime1(tmp1));

        return sb.toString();
    }

    private static String formatTime1(int time) {
        if (time < 10)
            return "0" + time;
        return "" + time;
    }


    public static int dipToPixel(Context context, int dipValue) {
        if (context == null) {
            return dipValue; // 原值返回
        }
        try {
            float pixelFloat = TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, dipValue, context
                            .getResources().getDisplayMetrics());
            return (int) pixelFloat;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dipValue;
    }

    /**
     * 计算预计到达目的地时间
     * 当前时间+剩余时间
     * @param time
     * @return
     */
    public static String calcArrivalTime(long time)
    {
		Date d = new Date(time);
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
		return dateFormat.format(d);
    }

    public static String getFriendlyLength(int lenMeter, DistanceUnit unit) {
        String unitString_km = ChString.Kilometer;
        String unitString_m = ChString.Meter;

        switch (unit){
            case Chinese:
                break;
            case English:
                unitString_m = "m";
                unitString_km = "km";
                break;
        }
        if (lenMeter > 10000) // 10 km
        {
            int dis = lenMeter / 1000;
            return dis + unitString_km;
        }

        if (lenMeter > 1000) {
            float dis = (float) lenMeter / 1000;
            DecimalFormat fnum = new DecimalFormat("##0.0");
            String dstr = fnum.format(dis);
            return dstr + unitString_km;
        }

        if (lenMeter > 100) {
            int dis = lenMeter / 50 * 50;
            return dis + unitString_m;
        }

        int dis = lenMeter / 10 * 10;
        if (dis == 0) {
            dis = 10;
        }

        return dis + unitString_m;
    }
    public static String getFriendlyTime(int second) {
        if (second > 3600) {
            int hour = second / 3600;
            int miniate = (second % 3600) / 60;
            return hour + "小时" + miniate + "分钟";
        }
        if (second >= 60) {
            int miniate = second / 60;
            return miniate + "分钟";
        }
        return second + "秒";
    }

    public static boolean isWifiConnected(Context context)
    {
        ConnectivityManager connectivityManager =
                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return wifiNetworkInfo.isConnected() ;
    }
}
